﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using System.Windows;

namespace ZombieMonkeys.Vibria.Audio
{
    /// <summary>
    /// Represents the functionality needed for loading sound files of .wav format
    /// </summary>
    public class SoundLoader
    {
        #region fields

        private static SoundLoader instance;

        
        Stream soundStream;
        static string loadDirectory = "";

        #endregion fields

        #region Properties
        /// <summary>
        /// Returns the specified loadDirectory for the object instance
        /// </summary>
        public static string LoadDirectory
        {
            get { return loadDirectory; }
        }

        #endregion Properties

        #region Constructors

        private SoundLoader()
        { 
        
        }

        private SoundLoader(string directoryToLoadFrom)
        {
            loadDirectory = directoryToLoadFrom;
        }

        #endregion Constructors

        #region PublicMethods

        public static SoundLoader GetSoundLoader(string directoryToLoadFrom)
        {
            if (instance == null)
            {
                instance = new SoundLoader(directoryToLoadFrom);
            }
            return instance;
        }

        /// <summary>
        /// Loads sound effect based on the string passed in
        /// </summary>
        /// <param name="soundName">the name of the .wav file as a string, do not include extension</param>
        /// <returns>A SoundEffect object</returns>
        //public SoundEffect LoadSound(string soundName)
        //{
        //    SoundEffect tempSoundEffect = content.Load<SoundEffect>(soundName);
        //    return tempSoundEffect;
        //}

        /// <summary>
        /// Gets a new SoundEffect based on a location, uses TitleContainer which looks at the default storage location
        /// </summary>
        /// <param name="soundName">fully qualified path name to exe and sound files, .wav only</param>
        /// <returns>A new SoundEffect object</returns>
        public SoundEffect GetSound(string soundName)
        {
            string path = Path.GetFullPath(soundName);
            
            Uri soundUri = new Uri(soundName, UriKind.Relative);

            SoundEffect soundFromStream;
            
            System.Windows.Resources.StreamResourceInfo streamInfo
                = System.Windows.Application.GetResourceStream(soundUri);

            //how do we know where this is going to be?
            //soundStream = TitleContainer.OpenStream(soundName);
            
            soundFromStream = loadSound(streamInfo.Stream);

            return soundFromStream;
        }

        #endregion PublicMethods

        #region privateMethods
        /// <summary>
        /// Loads a soundeffect based on a stream passed in
        /// </summary>
        /// <param name="soundStream">the current stream</param>
        /// <returns>A SoundEffect object</returns>
        private SoundEffect loadSound(Stream soundStream)
        {
            SoundEffect tempSoundEffect = SoundEffect.FromStream(soundStream);
            return tempSoundEffect;
        }

        #endregion privateMethods
    }
}
